package cn.glow.readingcloud.account.controller;


import cn.glow.readingcloud.account.pojo.UserRegister;
import cn.glow.readingcloud.account.dto.AuthDto;
import cn.glow.readingcloud.account.service.UserService;
import cn.glow.readingcloud.common.request.RequestParams;
import cn.glow.readingcloud.common.result.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Glow
 * @Date 2023-04-10 14:30:04
 * @Description
 * @Version 1.0
 */
@Api("用户服务接口")
@RestController
@RequestMapping("account/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "注册用户", httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = Result.class)})
    @PostMapping("/register")
    public Result register(@RequestBody UserRegister user) {
        return this.userService.register(user);
    }

    @ApiOperation(value = "用户登录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "loginName", value = "登录名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "登录密码", required = true, dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AuthDto.class)})
    @PostMapping("/login")
    public Result<AuthDto> login(@RequestBody RequestParams params) {
        String loginName = params.getStringValue("loginName");
        String password = params.getStringValue("password");
        return this.userService.login(loginName, password);
    }
    @ApiOperation(value = "退出用户登录", httpMethod = "DELETE")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = Result.class)})
    @DeleteMapping("/logout")
    public Result logout(String token) {
        return this.userService.logout(token);
    }
    @ApiOperation(value = "更新用户数据", httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = Result.class)})
    @PostMapping("/update")
    public Result update(@RequestBody AuthDto authDto) {
        return this.userService.update(authDto);
    }
}
